/**
 * Copyright (c) 2013-5-6 上午10:00:12 WKM and/or its affiliates. 
 * Use is subject to license terms.
 * All rights reserved.
 */
package org.extblog.wkm.web.dao.upload;

import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import org.extblog.wkm.web.vo.FileUploadView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.support.AbstractLobCreatingPreparedStatementCallback;
import org.springframework.jdbc.support.lob.LobCreator;
import org.springframework.jdbc.support.lob.LobHandler;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author WKM
 * Service.java history
 * 1.WKM - 2013-5-6 - Create File
 */
public class LobCreatingPreparedStatementCallback extends
		AbstractLobCreatingPreparedStatementCallback {
	
	/** 日志输出 **/
	protected Logger log = LoggerFactory.getLogger(getClass());
	
	
	private MultipartFile fileContent = null;
	
	private FileUploadView view = null;
	
	public LobCreatingPreparedStatementCallback(LobHandler lobHandler) {
		// TODO Auto-generated constructor stub
		super(lobHandler);
	}
	
	public MultipartFile getFileContent() {
		return fileContent;
	}
	public void setFileContent(MultipartFile fileContent) {
		this.fileContent = fileContent;
	}
	public FileUploadView getView() {
		return view;
	}
	public void setView(FileUploadView view) {
		this.view = view;
	}



	/* (non-Javadoc)
	 * @see org.springframework.jdbc.core.support.AbstractLobCreatingPreparedStatementCallback#setValues(java.sql.PreparedStatement, org.springframework.jdbc.support.lob.LobCreator)
	 */
	@Override
	protected void setValues(PreparedStatement ps, LobCreator lobCreator)
			throws SQLException, DataAccessException {
		// TODO Auto-generated method stub
		ps.setString(1, this.view.getFileId());   
        ps.setString(2, this.fileContent.getOriginalFilename());
        try {
			lobCreator.setBlobAsBytes(ps, 3, fileContent.getBytes());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			log.error("存储blob异常:" + e.getMessage());
		}
	}

}
